<script type="text/javascript">
	function AddRole() {
		$('#roleform').attr('action','{{urlfor "Admin.UserController.AddRole"}}');
		clearForm($('#roleform'));
		$('#RoleEdit').modal('show');
	}

	function EditRole(id) {
		$('#roleform').attr('action','{{urlfor "Admin.UserController.UpdateRole"}}');
		clearForm($('#roleform'));
		$.getJSON('{{urlfor "Admin.UserController.GetRole"}}',{id:id},function (data) {
			buildForm($('#roleform'),data)
		});
		$('#RoleEdit').modal('show');
	}

	function SaveForm() {
		$('#roleform').submit();
	}

	function sortChange(sort) {
		$('#search_sort').val(sort);
		$('#searchfrom').submit();
	}

	function clearForm(objE){  
		$(objE).find(':input').each(
			function(){
				switch(this.type){
					case 'hidden':
					case 'passsword':
					case 'select-multiple':
					case 'select-one':
					case 'text':
					case 'textarea':
						$(this).val('');
						break;
					case 'checkbox':
					case 'radio':
						this.checked = false;
				}
			}
		);
	}
	function buildForm(objE,data){ 
		$(objE).find(':input').each(
			function(){
				switch(this.type){
					case 'hidden':
					case 'passsword':
					case 'select-multiple':
					case 'select-one':
					case 'text':
					case 'textarea':
						$(this).val(data[this.name]);
						break;
					case 'radio':
						this.checked = (this.value == data[this.name]);
						break;
					case 'checkbox':
                		this.checked = ((',' + data[this.name] + ',').indexOf(',' + $(this).val() + ',') >= 0);
						break;
				}
			}
		);
	}
</script>